import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
  
/* 
 * 每对顶点间最短距离——Floyd-Warshall算法 
 * 注意：下标从1开始算起 
 */  
public class Main {  
      
    private static int N; //farms #  
    private static int M; //roads #  
    private static int X; //target farm  
      
    private static int w[][];//有向加权图  
    private static int d[][];//最短路径  
      
    private static void Floyd(){  
        /* 
         * 所有结点的编号依次为1,2, ... ,N 
         * 对任意i,j，依次i~~>j的最短路径的中间节点是：(k=1, ... ,N) 
         * ;       (没有中间节点) 
         * 1; 
         * 1,2; 
         * ... 
         * 1,2,...,N-1  
         */  
        for(int k=1;k<=N;k++){  
            for(int i=1;i<=N;i++){  
                for(int j=1;j<=N;j++){  
                    if(d[i][k]+d[k][j]<d[i][j]){  
                        d[i][j]=d[i][k]+d[k][j];  
                        //System.out.println("here");  
                    }  
                }  
            }  
        }  
    }  
      
    public static void main(String[] args) {  
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
        int maxLen=0;  
        try {  
            String[] data=br.readLine().split(" ");  
            N=Integer.parseInt(data[0]);  
            M=Integer.parseInt(data[1]);  
            X=Integer.parseInt(data[2]);  
              
            /* 
             * 初始化 
             */  
            w=new int[N+1][N+1];   
            d=new int[N+1][N+1];  
              
            for(int i=1;i<=N;i++){  
                for(int j=1;j<=N;j++){  
                    if(i==j){  
                        d[i][j]=w[i][j]=0;  
                    }  
                    else{  
                        d[i][j]=w[i][j]=Integer.MAX_VALUE/2;  
                    }  
                }  
            }  
              
            while(M>0){  
                data=br.readLine().split(" ");  
                d[Integer.parseInt(data[0])][Integer.parseInt(data[1])]  
                                             =w[Integer.parseInt(data[0])][Integer.parseInt(data[1])]  
                                                                           =Integer.parseInt(data[2]);  
                  
                M--;  
            }  
              
            Floyd();  
              
            maxLen=d[1][X]+d[X][1];  
            for(int i=2;i<=N;i++){  
                if(d[i][X]+d[X][i]>maxLen)  
                    maxLen=d[i][X]+d[X][i];  
            }         
            System.out.println(maxLen);  
              
        } catch (IOException e) {  
        }  
    }  
  
}  